package models;

import java.util.ArrayList;
import java.util.Collection;

import views.BattleFieldFrame;
import views.PlayerFrame;

import controllers.BattleThread;

public class BattleField {
	
	private ArrayList<Player> team1;
	private ArrayList<Monster> team2;
	private ArrayList<Buff>	buffs;
	
	private int width;
	private int height;
	
	private BattleFieldFrame outputFrame;
	private ArrayList<PlayerFrame> playerframes;
	private ArrayList<MonsterFrame> monsterframes;
	
	public BattleField( ArrayList<Player> team1, ArrayList<Monster> team2,
						int width, int height) {
		this.team1 = team1;
		this.team2 = team2;
		this.buffs = new ArrayList<Buff>();
		playerframes = new ArrayList<PlayerFrame>();
		monsterframes = new ArrayList<MonsterFrame>();
		this.width = width;
		this.height = height;
		
		outputFrame = new BattleFieldFrame(this.width, this.height);
		
		//place units and add frames
		if(team1 != null) {
			for(Player c : team1) {
				//place chars of team 1 (e.g. player)
				
				//lolcode
				c.x_pos = 10;
				c.y_pos = 10;
				
				playerframes.add(c.getPlayerFrame());
			}
		}
			
		if(team2 != null) {
			for(Monster c : team2) {
				//place chars of team 2 (e.g. enemies)
				
				//lolcode
				c.x_pos = 10;
				c.y_pos = 10;
				
				MonsterFrame newFrame = new MonsterFrame(300,200,c);
				c.setMonsterFrame(newFrame);
				monsterframes.add(newFrame);
			}
		}
	}
	
	public void start() {
		//start thread
		BattleThread bt1 = new BattleThread(this);
		bt1.start();
		
		//wait for winner
		try {
			bt1.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
			System.exit(92);
		}
		
		//start appropriate after-loss/win-windows
	}

	public ArrayList<Character> getCharlist() {
		ArrayList<Character> charlist = new ArrayList<Character>();
		if(team1 != null)
			charlist.addAll(team1);
		if(team2 != null)
			charlist.addAll(team2);
		return charlist;
	}

	public ArrayList<Buff> getBufflist() {
		return this.buffs;
	}

	public void removeBuffs(ArrayList<Buff> remove) {
		this.buffs.removeAll(remove);
	}
	
	public void addBuff(Buff b) {
		this.buffs.add(b);
	}

	public void reDraw() {
		this.outputFrame.repaint();
		for(PlayerFrame c : this.playerframes) {
			c.repaint();
		}
		for(MonsterFrame c : this.monsterframes) {
			c.repaint();
		}
	}
	
}
